function findStr(haystack, needle) {
  let start
  function findX(condition, nextState) {
    return function findNext(char) {
      if (char === condition) {
        return nextState
      } else {
        return start(char)
      }
    }
  }
  {
    let state = success
    for (let j = needle.length - 1; j--; j >= 0) {
      state = findX(needle[j], state)
    }
  }
  let state = start
  for (let i = 0; i < needle.length; i++) {
    state = state(haystack[i])
    if (state === success) {
      return i - needle.length + 1
    }
  }
  return -1
}

function success(c) {
  return success
}
